#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>

#define ARGS_CHECK(argc, num)               \
    {                                       \
        if (argc != num)                    \
        {                                   \
            fprintf(stderr, args error !n); \
            return -1;                      \
        }                                   \
    }
#define ERROR_CHECK(ret, num, msg) \
    {                              \
        if (ret == num)            \
        {                          \
            perror(msg);           \
            exit(1);               \
        }                          \
    }
int main(int argc, char *argv[])
{
    __pid_t pid = fork();
    if (pid == 0)
    {
        pid_t ret = setsid();
        ERROR_CHECK(-1, ret, "setsid");
        chdir("/");
        umask(0);
        int fdtab = getdtablesize();
        for (size_t i = 0; i < fdtab; i++)
        {
            close(i);
        }
        while (1)
        {
            __time_t tm;
            time(&tm);
            char timebuf[64];
            strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\n", localtime(&tm));
            FILE *fp = fopen("/home/leao/work/sdSys/linux/process/time.txt", "a+");
            ERROR_CHECK(NULL, fp, "fopen");
            fputs(timebuf, fp);
            fclose(fp);
            sleep(1);
        }
    }

    return 0;
}
